package band.wukong.practice.springcloud.quickstart.qk04.ribbon;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author wukong(wukonggg@139.com)
 */
@Service
public class HelloService {

    @Value("${app.serviceDirectory.hiService.uri}")
    private String uri;

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject(uri + "?name=" + name, String.class);
    }

    /**
     * 在qk-01-eureka-client不可用的情况下:
     * 如果qk-04-hystrix-with-ribbon调用qk-01-eureka-client的API接口，
     * 程序会执行快速失败，直接返回一组字符串，而不是等待响应超时，这很好的控制了容器的线程阻塞。
     */
    public String hiError(String name) {
        return "hi," + name + ". Sorry for error!";
    }
}
